import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * 注意:子菜单只在路由子菜单时出现。 长度> = 1
 * 明细详见表: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为true, item将不会显示在侧边栏中(默认为false)
 * alwaysShow: true               如果设置为true，将始终显示根菜单
 *                                当item有多个子路由时，如果没有设置alwaysShow,
 *                                它将成为嵌套模式，否则不显示根菜单
 * redirect: noRedirect           如果设置noRedirect将不会在面包屑中重定向
 * name:'router-name'             该名称由<keep-alive>使用(必须设置!!)
 * meta : {
    roles: ['admin','editor']    控制页面角色(您可以设置多个角色)
    title: 'title'               名称显示在侧边栏和面包屑(建议设置)
    icon: 'svg-name'/'el-icon-x' 图标显示在侧边栏中
    breadcrumb: false            如果设置为false，该项目将隐藏在breadcrumb中(默认为true)
    activeMenu: '/example/list'  如果设置了path，侧边栏将突出显示您设置的路径
  }
 */

/**
 * constantRoutes 表示不需要动态访问的路由，如登录页面、404、通用页面等
 * 没有权限要求的基本页
 * 所有角色都可以被访问
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: '首页',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/campus',
    component: Layout,
    redirect: '/campus',
    children: [
      {
        path: 'campus',
        name: '驾校管理',
        component: () => import('@/views/campus/index'),
        meta: { title: '驾校管理', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/car',
    component: Layout,
    redirect: '/car',
    children: [
      {
        path: 'car',
        name: '车辆管理',
        component: () => import('@/views/car/index'),
        meta: { title: '车辆管理', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/student',
    name: 'User',
    meta: { title: '用户管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'student',
        name: '学员管理',
        component: () => import('@/views/user/student'),
        meta: { title: '学员管理', icon: 'dashboard' }
      },
      {
        path: 'coach',
        name: '教练管理',
        component: () => import('@/views/user/coach'),
        meta: { title: '教练管理', icon: 'dashboard' }
      },
      {
        path: 'admin',
        name: '普通管理员',
        component: () => import('@/views/user/admin'),
        meta: { title: '普通管理员', icon: 'dashboard' }
      }
    ]
  },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user'),
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', icon: 'tree' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
